/* ------------------------------------------------------------------------
 * manage.js
 * Copyright (c) 2007-2009 Brainberry, LLC. All rights reserved.
 * ------------------------------------------------------------------------ */

Event.observe(window, 'load', function() {
  var e = $A(document.getElementsByTagName('*')).find(function(e) {
    return (e.tagName.toUpperCase() == 'INPUT' && (e.type == 'text' || e.type == 'password'))
        || e.tagName.toUpperCase() == 'TEXTAREA';
  });
  if (e) e.focus();
});

function fade_flash()
{
  var container = $('rightContent');
  var messages = container.select('div.message');
  
  for(var i = 0; i < messages.length; i++)
  {
    Effect.BlindUp(messages[i], {duration: 0.2});
  }
}

var Tree = {
	expand: function(event, mode)
	{
		node = Event.element(event);
		
		if (typeof(node) == 'undefined') return;

		//var li = node.up("li");
		//var uls = li.nextSiblings();
		//var li = node.up("ul");
		var cat_id = node.id.replace('a', 'cat_');
		var li = $(cat_id);
		var parent = null;
		var uls = li.getElementsByTagName('ul');
		
		if (typeof(mode) == 'undefined') mode = (node.className == 'minus');
		
		for(var i = 0; i < uls.length; i++)
		{
			parent = (uls[i].parentNode.tagName.toLowerCase() == 'ul' ? uls[i].parentNode : $(uls[i]).up('ul'));
			
			if (parent && parent.id == li.id)
			{
				if (mode)	{uls[i].hide();} else {uls[i].show();}
				//mode ? uls[i].hide() : uls[i].hide();
			}
		}
		node.className = (mode) ? 'plus' : 'minus';

		this.save(node.id, (mode ? 0 : 1));
		Event.stop(event);
	},
	
	init: function(tree)
	{
		this.tree = $(tree);
		this.cookie = new wp_Cookie({ expires : 6, expires_unit : "months"});
		this.cookie_key = "manage_tree";
		
		var parent = null;
		var links = this.tree.select("a.minus");
		links.shift();
		
		for(var i = 0; i < links.length; i++)
		{
			node = $(links[i]);

			if (typeof(node) != 'undefined')
			{
				//var li = node.up("ul");
				var cat_id = node.id.replace('a', 'cat_');
				var li = $(cat_id);
				//var uls = li.nextSiblings();
				var uls = li.getElementsByTagName('ul');
			
				mode = !this.state(node.id);
				
				for(var j = 0; j < uls.length; j++)
				{
					parent = (uls[j].parentNode.tagName.toLowerCase() == 'ul' ? uls[j].parentNode : $(uls[j]).up('ul'));
			
					if (parent && parent.id == li.id)
					{
						if (mode)	{uls[j].hide();} else {uls[j].show();}
						//mode ? uls[i].hide() : uls[i].hide();
					}
					
					parent = null;
				}
				
				node.className = (mode) ? 'plus' : 'minus';
			}
		}
	},
	
	save: function(node_key, value)
	{	
		var str = this.cookie.get(this.cookie_key);
		var mas = new Array();
		
		if (str == null) str = '';
		
		var arr = str.split(';');
		
		mas.push(node_key + ":" + value);
		
		for(var i = 0; i < arr.length; i++)
		{
			hash = arr[i].split(':');
			if (hash[0] != node_key)
			{
				mas.push(arr[i]);
			}
		}
		
		str = mas.join(';');
		this.cookie.set(this.cookie_key, str);
	},
	
	state: function(node_key)
	{
		var str = this.cookie.get(this.cookie_key);
		if (str == null) str = '';
		var value = 0;
		
		var arr = str.split(';');
		for(var i = 0; i < arr.length; i++)
		{
			hash = arr[i].split(':');
			if (hash[0] == node_key)
			{
				value = parseInt(hash[1]);
				break;
			}
		}
	
		return value == 1;
	}
	
};

function make_selected_widget(text, li) 
{
  var element = $(li);
  $('widget_widgetable_id').value = element.id;
}
